{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import os\n",
    "from netCDF4 import Dataset\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "from osgeo import gdal\n",
    "import scipy.io as sio"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Option 1: Load data from a geoTIFF file\n",
    "<pre>\n",
    "gdata = gdal.Open(<span style=\"color:#cc7a00\"><b>'GEOTIFF_FILE.tif'</b></span>)\n",
    "geo = gdata.GetGeoTransform()\n",
    "data = gdata.ReadAsArray()\n",
    "</pre>\n",
    "\n",
    "#### Option 2: Load data from a Matlab file\n",
    "<pre>\n",
    "matfile  = sio.loadmat(<span style=\"color:#cc7a00\"><b>'MATLAB_FILE.mat'</b></span>)\n",
    "data = matfile[<span style=\"color:#cc7a00\"><b>'VARIABLE_TOPOGRAPHY'</b></span>]\n",
    "</pre>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "matfile  = sio.loadmat('EBI_topo.mat')\n",
    "data = matfile['Ori_DEM']\n",
    "\n",
    "Topography_input = data\n",
    "# Check/create folder and topography file name\n",
    "folder = 'data'\n",
    "filename_output = 'topography.nc'\n",
    "if not os.path.exists(folder):\n",
    "    os.makedirs(folder)\n",
    "\n",
    "################################################################\n",
    "# DO NOT CHANGE BELOW THIS LINE --------------------------------\n",
    "################################################################\n",
    "\n",
    "topography = Dataset(folder + '/' + filename_output, 'w', format='NETCDF4')\n",
    "topography.description = 'Topography Data for Dhara'\n",
    "\n",
    "# Get dimension\n",
    "My,Nx  = Topography_input.shape\n",
    "\n",
    "# Set up dimension for topography in NetCDF\n",
    "topography.createDimension('y', My)\n",
    "topography.createDimension('x', Nx)\n",
    "\n",
    "\"\"\"\n",
    "Create variables in the netcdf file\n",
    "    var = netcdf.createVariable('Var_name', 'var_type', ('dimension_type'))\n",
    "\"\"\"\n",
    "Ztopo = topography.createVariable('Topography', 'f8', ('y','x'))\n",
    "\n",
    "# Assign data to variables in NetCDF file\n",
    "Ztopo[:] = Topography_input\n",
    "\n",
    "# Close the file\n",
    "topography.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### If you want to visualize topography, run the command below"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Plot topography for checking\n",
    "fig,ax = plt.subplots(figsize=(12,8))\n",
    "im = ax.imshow(Topography_input, cmap=plt.cm.jet)\n",
    "fig.colorbar(im, ax=ax)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
